<template>
  <a-drawer v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
            :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
            :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
            :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">
    <!--表单开始-->

    <a-form ref="formRef" :model="formState" layout="vertical" :rules="RULES">
      <a-row :gutter="[12,12]">
        <a-col :span="12">
          <a-form-item label="机构代码" field="code">
            <a-input v-model="formState.code" placeholder="请填写机构代码" :disabled="isUpdate"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="机构名称" field="name">
            <a-input v-model="formState.name" placeholder="请填写机构名称"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上级机构" field="parentCode">
            <smart-tree-selector v-model="formState.parentCode" :url="SystemOptions.OrganTreeOptions" placeholder="请选上级机构" top-label="顶级机构"/>
          </a-form-item>
        </a-col>


        <a-col :span="12">
          <a-form-item label="值班电话" field="tel">
            <a-input v-model="formState.tel" placeholder="请填写值班电话"/>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="详细地址" field="address">
            <smart-addr v-model="formState.address"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item :label="`显示排序值 [ ${formState.sort} ]` " field="sort" tooltip="升序排序 显示值越小 显示越靠前">
            <a-slider v-model="formState.sort"/>
          </a-form-item>

        </a-col>
      </a-row>


    </a-form>
    <!--表单结束-->
  </a-drawer>
</template>

<script lang="ts" setup>

import {watch} from "vue";
import {EditDrawer} from '@/libs/core'
import {SystemOptions} from '@/libs/options'
import {ORGAN_EDIT} from "../resource/form";
import {SystemHttp} from '../resource/http'


const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])
const {BODY, RULES} = ORGAN_EDIT

const {addOrgan, updateOrgan} = SystemHttp
const config = {tplRecord: BODY, rawRecord: props.record, rowKey: 'id', emit}
const service = new EditDrawer(config).setAddRequest(addOrgan).setUpdateRequest(updateOrgan)
const {formRef, formState, LAYER, isUpdate, onCancel, onSubmit} = service
LAYER.title = isUpdate ? '新建机构' : '修改机构'

// 位置属性发生变化的时候重新执行一次表单验证
watch(() => formState.value.address, () => formRef.value.validate())
</script>
